package servlet;

import bean.UserLogin;
import service.LoginService;
import service.RegService;
import serviceimpl.LoginServiceimpl;
import serviceimpl.RegServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RegServlet", value = "/RegServlet")
public class RegServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RegService regService =new RegServiceImpl();
        //得到用户名和密码和用户类型
        int usertype=1;
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String user_type=request.getParameter("usertype");
        String re_password=request.getParameter("re_password");
        System.out.println(username+password+usertype+re_password);
        PrintWriter writer = response.getWriter();
        //判断登录
        if(username!="" && password!="" && re_password!="" && user_type!="" && password.equals(re_password)==true){
            //根据用户类型转换成数据库对应的整型
            if(user_type.equals("普通用户")){
                usertype=1;
            }
            else
                usertype=2;
            UserLogin userLogin=new UserLogin(username,password,usertype);
            //成功跳转到主页面
            if(regService.isReg(userLogin))
                writer.println("ok");
                //失败跳转到登录页面
            else
                writer.println("error");
        }
        //失败跳转到登录页面
        else
            writer.println("error");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
    }
}
